package com.ruoyi.web.controller.lingshui.project;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supervise.domain.BaseConstructionPermit;
import com.ruoyi.supervise.domain.BaseProjectInfo;
import com.ruoyi.supervise.domain.BaseUnitEngineeringInfo;
import com.ruoyi.supervise.domain.BaseUnitEngineeringPartInfo;
import com.ruoyi.supervise.domain.CompletionRecordTable;
import com.ruoyi.supervise.domain.ConDrawingTable;
import com.ruoyi.supervise.domain.ConstructionPermit;
import com.ruoyi.supervise.domain.ProjectArchives;
import com.ruoyi.supervise.domain.SurveyResults;
import com.ruoyi.supervise.domain.UnitEngineeringInfoTable;
import com.ruoyi.supervise.service.IBaseProjectInfoService;
import com.ruoyi.supervise.service.IBaseUnitEngineeringInfoService;
import com.ruoyi.supervise.service.IBaseUnitEngineeringPartInfoService;
import com.ruoyi.supervise.service.ICompletionRecordService;
import com.ruoyi.supervise.service.IConDrawingService;
import com.ruoyi.supervise.service.IConstructionPermitService;
import com.ruoyi.supervise.service.IProjectArchivesBaseInfoService;
import com.ruoyi.supervise.service.IProjectArchivesService;
import com.ruoyi.supervise.service.ISurveyResultsService;

import cn.hutool.core.util.StrUtil;

/**
 * @ClassName: projectAllController
 * @Description: 项目档案库controller
 * @Author 吴挺杰
 * @DateTime 2019年11月11日 下午3:53:43
 */
@Controller
@RequestMapping("/projectArchives/projectArchives")
public class ProjectArchivesController extends BaseController{

	private String prefix = "lingshui/projectArchives";
	
	@Autowired
    private IProjectArchivesService projectArchivesService;
	
	@Autowired
    private IProjectArchivesBaseInfoService projectArchivesBaseInfoService;
	
	@Autowired
    private ISurveyResultsService surveyResultsService;
	
	@Autowired
    private IConDrawingService conDrawingService;
	
	@Autowired
    private IConstructionPermitService constructionPermitService;
	
	@Autowired
    private ICompletionRecordService completionRecordService;
	
	@Autowired
	private IBaseUnitEngineeringInfoService baseUnitEngineeringInfoService;
	
	@Autowired
	private IBaseProjectInfoService baseProjectInfoService;
	
	@Autowired
	private IBaseUnitEngineeringPartInfoService baseUnitEngineeringPartInfoService;
	
	/**
	 * @Title: projectAll
	 * @Description: 跳转到项目档案库页面
	 * @Author wtj
	 * @DateTime 2020-9-27 15:35:35
	 * @return
	 */
	@RequiresPermissions("project:projectAll:view")
	@GetMapping()
	public String projectAll()
	{
	    return prefix + "/projectArchives";
	}
	
	/**
	 * @Title: list
	 * @Description: 项目档案库table
	 * @Author wtj
	 * @DateTime 2020年3月16日 下午5:39:34
	 * @param projectArchives
	 * @return
	 */
    @RequiresPermissions("project:projectAll:lists")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProjectArchives projectArchives)
    {
        startPage();
        List<ProjectArchives> projectArchivesList = projectArchivesService.selectProjectArchivesList(projectArchives);
        //数据库中Base_ProjectInfo表中的ConstructUnitId字段的类型如下:"|海南定安天九美食文化广场有限公司"是正确的ConstructUnitId，其他的都是非法的
        projectArchivesList.forEach(pa ->{
        	String[] subStr=StrUtil.split(pa.getConstructUnitId(), "|");
        	if(subStr.length == 2) {
        		pa.setConstructUnitId(subStr[1]);
        	}else {
        		pa.setConstructUnitId(" ");
        	}
        });
        return getDataTable(projectArchivesList);
    }
    
    /**
     * @Title: detail
     * @Description: 项目详情
     * @Author wtj
     * @DateTime 2020年3月16日 下午5:38:43
     * @param projectArchivesBaseInfo
     * @param mmap
     * @return
     */
    @RequiresPermissions(value={"project:projectAll:basic", "project:projectAll:map", "project:projectAll:unit", "surResult:surResult:list", "supervise:constructionDrawingRecord:list", "conpermit:conpermit:list", "completion:completion:list"}, logical=Logical.OR)
   @GetMapping("/detail/{guid}")
   public String detail(@PathVariable("guid") String guid,BaseProjectInfo projectArchivesBaseInfo,HttpSession session,ModelMap mmap)
   {
	   projectArchivesBaseInfo.setGuid(guid);
	   BaseProjectInfo pabi=projectArchivesBaseInfoService.selectProjectArchivesBaseInfoByGuid(projectArchivesBaseInfo);
	   boolean is=StrUtil.containsAnyIgnoreCase(pabi.getProjectType(), "市政");
	   if(pabi.getProjectType().equals("私人住宅")) {
		   mmap.put("projectType", "私人住宅"); 
	   }else {
		   if(is) {
			   mmap.put("projectType", "市政基础设施工程");  
		   }else {
			   mmap.put("projectType", "房屋建筑工程"); 
		   }
	   }
	   mmap.put("pabi", pabi);
	   mmap.put("isszfj", is);
	   mmap.put("proGuid", projectArchivesBaseInfo.getGuid());
	   session.setAttribute("proGuid", projectArchivesBaseInfo.getGuid());
	   session.setAttribute("isszfj", is);
	   //下方的是为了展示年前的demo所弄的demo数据
	   session.setAttribute("projectName", "田德海泉湾A区");
	   session.setAttribute("projectCompany", pabi.getConstructUnitId());
       session.setAttribute("projectPrice", pabi.getProjectCost());
       session.setAttribute("projectArea", pabi.getProjectScale());
       session.setAttribute("projectAddress", pabi.getProjectAddress());
       mmap.put("projectName", "田德海泉湾A区");
       mmap.put("projectCompany", pabi.getConstructUnitId());
       mmap.put("projectPrice", pabi.getProjectCost());
       mmap.put("projectArea", pabi.getProjectScale());
       mmap.put("projectAddress", pabi.getProjectAddress());
	   return prefix + "/projectArchivesDetail";
   }
   
   /**
    * @Title: list
    * @Description: 获取某个项目下单位工程table数据
    * @Author wtj
    * @DateTime 2020年3月16日 下午6:07:11
    * @param projectArchives
    * @return
    */
   @RequiresPermissions("project:projectAll:unit")
   @PostMapping("/unitEngineeringInfoTableList/{guid}/{isszfj}")
   @ResponseBody
   public TableDataInfo unitEngineeringInfoTableList(@PathVariable("guid") String guid,@PathVariable("isszfj") String isszfj,HttpSession session)
   {
       startPage();
       UnitEngineeringInfoTable unitEngineeringInfoTable = new UnitEngineeringInfoTable();
       unitEngineeringInfoTable.setProjectGuid(guid);
       List<UnitEngineeringInfoTable> unitEngineeringInfoTableList = new ArrayList<UnitEngineeringInfoTable>();
       if(isszfj.equals("true")) {
    	   unitEngineeringInfoTableList = projectArchivesBaseInfoService.selectUnitEngineeringInfoTableSZList(unitEngineeringInfoTable);
       }else {
    	   unitEngineeringInfoTableList = projectArchivesBaseInfoService.selectUnitEngineeringInfoTableFJList(unitEngineeringInfoTable);
       }
       //下面是获取单位工程的状态，按照顺序，联查竣工备案，施工许可，哪个有状态，就显示哪个状态，如果两个都有状态，就按照顺序显示，竣工有，就显示竣工的状态
       if(!StringUtils.isEmpty(unitEngineeringInfoTableList)) {
    	   for(UnitEngineeringInfoTable uei:unitEngineeringInfoTableList) {
    		   if(!StringUtils.isEmpty(uei.getUnitEngineeringState())) {
    			   if(uei.getUnitEngineeringState().equals("在建")) {
        			   String cr = completionRecordService.selectCompletionRecordStateByEngGuid(uei.getGuid());
                	   if(!StringUtils.isEmpty(cr)) {
                		   uei.setUnitEngineeringState("(竣工备案("+cr+"))");
                	   }else {
                		   String cp = constructionPermitService.selectBaseConstructionPermitStateByUnitEngId(uei.getGuid());
                		   if(!StringUtils.isEmpty(cp)) {
                			   uei.setUnitEngineeringState("(施工许可("+cp+"))");
                		   }else {
                			   uei.setUnitEngineeringState(" ");
                		   }
                	   }
        		   }
    		   }
           }
       }
       return getDataTable(unitEngineeringInfoTableList);
   }
   
   /**
    * @Title: unitEngineeringDetail
    * @Description: 单位工程详情
    * @Author wtj
    * @DateTime 2020年3月27日 上午11:45:30
    * @param guid
    * @param mmap
    * @return
    */
   @RequiresPermissions("project:projectAll:unitdetail")
   @GetMapping("/unitEngineeringDetail/{guid}/{isszfj}")
   public String unitEngineeringDetail(@PathVariable("guid") String guid,@PathVariable("isszfj") String isszfj, ModelMap mmap)
   {
	   BaseUnitEngineeringInfo baseUnitEngineeringInfo = baseUnitEngineeringInfoService.selectBaseUnitEngineeringInfoByGuid(guid);
	   mmap.put("buei", baseUnitEngineeringInfo);
	   BaseProjectInfo baseProjectInfo = baseProjectInfoService.selectBaseProjectInfoByGuid(baseUnitEngineeringInfo.getProjectGuid());
	   mmap.put("bpi", baseProjectInfo);
	   BaseUnitEngineeringPartInfo buepi = baseUnitEngineeringPartInfoService.selectBaseUnitEngineeringPartInfoByGuid(guid);
	   mmap.put("buepi", buepi);
	   List<BaseConstructionPermit> bcpList = constructionPermitService.selectBaseConstructionPermitByUnitEngId(guid);
	   if(!StringUtils.isEmpty(bcpList)) {
		   mmap.put("bcp", bcpList.get(0));
	   }else {
		   mmap.put("bcp", null);
	   }
	   mmap.put("isszfj", isszfj);
	   return prefix + "/projectArchivesUnitDetail";
   }
   
   /**
    * @Title: surveyResultsTableIfoList
    * @Description: 获取某个项目下勘察成果table数据
    * @Author wtj
    * @DateTime 2020年3月27日 上午11:46:01
    * @param guid
    * @param session
    * @return
    */
   @RequiresPermissions("surResult:surResult:list")
   @PostMapping("/surveyResultsTableIfoList/{guid}")
   @ResponseBody
   public TableDataInfo surveyResultsTableIfoList(@PathVariable("guid") String guid)
   {
       startPage();
       SurveyResults surveyResults = new SurveyResults();
       surveyResults.setProjectGuid(guid);
       List<SurveyResults> surveyResultsList=surveyResultsService.selectSurveyResultsList(surveyResults);
       return getDataTable(surveyResultsList);
   }
   
   /**
    * @Title: ConDrawingTableIfoList
    * @Description: 获取摸个项目下施工图审查table数据
    * @Author wtj
    * @DateTime 2020年3月27日 上午11:46:44
    * @param guid
    * @param session
    * @return
    */
   @RequiresPermissions("supervise:constructionDrawingRecord:list")
   @PostMapping("/conDrawingTableIfoList/{guid}")
   @ResponseBody
   public TableDataInfo ConDrawingTableIfoList(@PathVariable("guid") String guid)
   {
       startPage();
       ConDrawingTable conDrawingTable = new ConDrawingTable();
       conDrawingTable.setProjectGuid(guid);
       List<ConDrawingTable> conDrawingTableList=conDrawingService.selectConDrawingTableList(conDrawingTable);
       return getDataTable(conDrawingTableList);
   }
   
   /**
    * @Title: conpermitTableIfoList
    * @Description: 获取某个项目下施工许可table数据
    * @Author wtj
    * @DateTime 2020年3月27日 上午11:47:07
    * @param guid
    * @param session
    * @return
    */
   @RequiresPermissions("conpermit:conpermit:list")
   @PostMapping("/conpermitTableIfoList/{guid}")
   @ResponseBody
   public TableDataInfo conpermitTableIfoList(@PathVariable("guid") String guid)
   {
       startPage();
       ConstructionPermit constructionPermit = new ConstructionPermit();
       constructionPermit.setProGuid(guid);
       List<ConstructionPermit> constructionPermitList=constructionPermitService.selectConstructionPermitList(constructionPermit);
       return getDataTable(constructionPermitList);
   }
   
   /**
    * @Title: completionTableIfoList
    * @Description: 获取某个项目下竣工备案table数据
    * @Author wtj
    * @DateTime 2020年3月27日 上午11:47:37
    * @param guid
    * @param session
    * @return 
    */
   @RequiresPermissions("completion:completion:list")
   @PostMapping("/completionTableIfoList/{guid}")
   @ResponseBody
   public TableDataInfo completionTableIfoList(@PathVariable("guid") String guid)
   {
	   startPage();
       CompletionRecordTable completionRecordTable = new CompletionRecordTable();
       completionRecordTable.setProjectInfoGuid(guid);
       List<CompletionRecordTable> completionRecordTableList=completionRecordService.selectCompletionRecordTableList(completionRecordTable);
       return getDataTable(completionRecordTableList);
   }
   
   
}
